<template>
  <div class="alarm-scroll">
    <div class="alarm-box" v-for="(item, index) in arr" :key="index">
      <div class="alarm">
        <div class="alarm-title">
          <div>设备预警</div>
          <div @click="handlerClose(item)">
            <i class="el-icon-close"></i>
          </div>
        </div>
        <div class="alarm-content">
          <div class="alarm-notice">
            <div class="inline wrap">
              <div>{{ item.name1 }}</div>
              <div>—{{ item.name2 }}</div>
              <div>—{{ item.name3 }}</div>
            </div>
            <div class="inline nowrap">
              <dict-tag
                id="dictTag"
                :options="dict.type.device_alarm_type"
                :value="item.type"
              />
              <div class="time">{{ item.time }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    arr: {
      type: Array,
      default: [],
    },
  },
  dicts: [
    "device_alarm_type", // 设备警报类型
  ],
  data() {
    return {};
  },
  created() {},
  mounted() {},
  watch: {
    arr: {
      handler(val) {
        if (val) {
          if (this.arr.length > 0) {
            // console.log(this.arr, "报警数组");
            this.arr.map((item) => {
              this.openNewintval(item);
            });
          }
        }
      },
      deep: true,
      immediate: false,
    },
  },
  methods: {
    openNewintval(item) {
      let closetimer = setInterval(() => {
        this.closejudge(item);
      }, 1000);
      this.$once("hook:beforeDestroy", () => {
        clearInterval(closetimer);
        closetimer = null;
      });
    },
    closejudge(item) {
      let time = Date.now();
      if (item.closeTime <= time) {
        this.handlerClose(item);
      }
    },
    handlerClose(item) {
      this.$emit("closeAlarm", item.id);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/styles/mixin.scss";
.alarm-scroll {
  max-height: 330px;
  overflow: hidden;
  overflow-y: auto;
  @include scrollBar2;
}
.alarm-box {
  background-color: #ffffff;
  z-index: 99;
  border-radius: 8px;
  box-sizing: border-box;
  border: 1px solid #ebeef5;
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  overflow: hidden;
  &:not(:nth-child(1)) {
    margin-top: 20px;
  }
}
.alarm {
  margin: 10px 20px;
  width: 280px;
  .alarm-title {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: rgba(34, 34, 34, 0.85);
    line-height: 24px;
    .el-icon-close {
      cursor: pointer;
    }
  }
  .alarm-content {
    width: 100%;
    max-height: 85px;
    overflow: hidden;
    overflow-y: auto;
    @include scrollBar2;
    .alarm-notice {
      &:not(:last-child) {
        margin-bottom: 20px;
      }
      .inline {
        display: flex;
      }
      .wrap {
        flex-wrap: wrap;
        div {
          color: #000000;
          font-size: 12px;
        }
      }
      .nowrap {
        flex-direction: column;
        align-items: flex-start;
        flex-wrap: wrap;
        .time {
          color: #000000;
          font-size: 12px;
        }
        #dictTag {
          margin: 5px 0;
          margin-left: -10px !important;
        }
      }
    }
  }
}
</style>
